import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.junit.Test;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dongyuncong on 2017/6/20.
 */
public class HengXinYongLi {

    private static final Logger log = Logger.getLogger(ParameterClient.class);

    @Test
    public void apply() {
        String url = "http://61.144.244.169:18050/api/HXUsers/InHxUserInfo";

        Map<String, Object> params = new HashMap<String, Object>();

        Map<String, String> userInfo = new HashMap<String, String>();               //用户基本资料信息


        /*Required*/
        userInfo.put("RealName", "王小二");        //真实姓名
        userInfo.put("Sex", "0");        //性别 0是男，1是女
        /*None*/
        userInfo.put("CardNo", "220524198801010299");        //身份证编号
        userInfo.put("Tel", "18565846589");        //手机号
        userInfo.put("Province", "广东");        //省
        userInfo.put("City", "深圳");        //市级

        userInfo.put("CarStatus", "1");        //个人资产-车状态，0为无，1为全款，2为按揭
        userInfo.put("HouseStatus", "0");        //个人资产-房状态：0为无，1为全款，2为按揭


        userInfo.put("UserType", "0");        //用户类型 0:工薪阶层1:私营业主
        userInfo.put("UserStatus", "0");        //用户状态 0:启用1:禁用2:锁定
//        userInfo.put("UserFrom","11");        //用户渠道  KaNiuData 11:卡牛
        userInfo.put("IsAuthentication", "1");        //是否实名认证 0.未认证，1.已认证

//        contacts.put()










        params.put("userInfo", userInfo);
        params.put("contacts", new JSONArray());
        params.put("mailList", new JSONArray());
        params.put("bankFlowList", new JSONArray());
        JSONObject jsonObject = JSONObject.fromObject(params);


        try {
            System.out.println(jsonObject.toString());
            String result = "";//HttpClientTools.postJson(new HttpRequestData(), null, url, jsonObject, 1000);
            JSONObject object = JSONObject.fromObject(result);
            System.out.println(result);
        } catch (Exception e) {
            System.out.println("error: "+e.getMessage());
        }
    }

    private String urlEncoderTwoTimes(String toEncoder) {
        String res = "";
        try {
            res = URLEncoder.encode(URLEncoder.encode(toEncoder, "UTF-8"), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            log.error("======Encoder error ========" + e.getMessage());
        }
        return res;
    }

    @Test
    public void test(){
        List<Object> list=new ArrayList<>();
        Map<String, String> userInfo = new HashMap<String, String>();               //用户基本资料信息
        userInfo.put("RealName", "王小二");        //真实姓名
        userInfo.put("Sex", "0");        //性别 0是男，1是女
        userInfo.put("CardNo", "220524198801010299");        //身份证编号
        userInfo.put("Tel", "18565846589");
        list.add(userInfo);
        list.add(userInfo);
        list.add(userInfo);
        list.add(userInfo);
        Map<String, Object> params = new HashMap<String, Object>();
        ((HashMap) params).put("num",list);


        System.out.println(JSONObject.fromObject(params).toString());


        Map<String, Object> users = new HashMap<>();
        users.put("0",userInfo);
        users.put("1",userInfo);
        users.put("2",userInfo);
        users.put("3",userInfo);
        /*       
        Map<Map<>>使用JSONArray.fromObject()但由于 key所以 json包含key,外层一个方括号，数组一个元素
        [{"0":{"CardNo":"220524198801010299","Sex":"0","Tel":"18565846589","RealName":"王小二"},"1":{"CardNo":"220524198801010299","Sex":"0","Tel":"18565846589","RealName":"王小二"},"2":{"CardNo":"220524198801010299","Sex":"0","Tel":"18565846589","RealName":"王小二"},"3":{"CardNo":"220524198801010299","Sex":"0","Tel":"18565846589","RealName":"王小二"}}]
        */
        System.out.println(JSONArray.fromObject(users));
        /*
        Map<Map<>>使用JSONObject.fromObject() 上例去掉两边方括号
        {"0":{"CardNo":"220524198801010299","Sex":"0","Tel":"18565846589","RealName":"王小二"},"1":{"CardNo":"220524198801010299","Sex":"0","Tel":"18565846589","RealName":"王小二"},"2":{"CardNo":"220524198801010299","Sex":"0","Tel":"18565846589","RealName":"王小二"},"3":{"CardNo":"220524198801010299","Sex":"0","Tel":"18565846589","RealName":"王小二"}}
         */
        System.out.println(JSONObject.fromObject(users));

        // TODO: 2017/6/21 生成JSON数组还是要用Map<String,List<Map<String,String>>>
    }


}
